<?php

namespace App\Http\Controllers\Seller;

use App\Http\Requests\Seller\User\CreateRequest;
use App\Http\Requests\Seller\User\UpdateRequest;
use App\Models\User;
use App\Repositories\Seller\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;

class UserController extends BaseController
{
    protected $repo;
    public function __construct()
    {
        $this->repo = new UserRepository();
    }

    /**
     * 列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function fetchList(Request $request)
    {
        $where['seller_id'] = $this->id();
        $status = $request->get('status',-1);
        $mobile = $request->get('mobile','');
        if($status>-1){
            $where['status'] = $status;
        }
        if($mobile){
            $where['mobile'] = $mobile;
        }
        $users = $this->repo->pagination($where,$request->all());
        return $this->success($users);
    }

    /**
     * 拉去单个信息
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     */
    public function fetchInfo(User $user){
        if($user->seller_id!=$this->id()){
            return $this->error('用户不存在',[],404,404);
        }
        return $this->success($user);
    }

    /**
     * 增
     * @param CreateRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(CreateRequest $request){
        $data = $request->only('mobile','nickname','desc');
        $seller = $this->auth()->user();
        $data['password'] = Config::get('seller.pwd');
        $data['status'] = true;
        $user = $this->repo->create($seller,$data);
        $user = $this->repo->model()->find($user->id);
        return !$user ? $this->error('添加失败') : $this->success($user, '添加成功');
    }

    /**
     * 改
     * @param UpdateRequest $request
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(UpdateRequest $request, User $user){
        $data = $request->only('nickname','desc','status');
        if($user->seller_id!=$this->id()){
            return $this->error('用户不存在',[],404,404);
        }
        $user = $this->repo->update($user,$data);
        return !$user?$this->error('更新失败'):$this->success($user,'更新成功');
    }
    public function delete(User $user){
        if($user->seller_id!=$this->id()){
            return $this->error('用户不存在',[],404,404);
        }
        $user->delete();
        return $this->success([],'删除成功');
    }
    /**
     * 更新状态
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     */
    public function setStatus(User $user){
        if($user->seller_id!=$this->id()){
            return $this->error('用户不存在',[],404,404);
        }
        $data['status'] = !$user->status;
        $this->repo->update($user,$data);
        return $this->success([],'状态修改成功');
    }
    /**
     * 重置密码
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     */
    public function resetPassword(User $user){
        if($user->seller_id!=$this->id()){
            return $this->error('用户不存在',[],404,404);
        }
        $pwd = $data['password'] = config('seller.pwd');
        $saved = $this->repo->update($user,$data);
        return !$saved?$this->error('重置失败'):$this->success([
            'password'=>$pwd
        ],"密码已重置，重置密码为:{$pwd}");
    }
}
